//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.svc;

import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.UploadDao;
import com.aidong.model.wills.Upload;
import com.aidong.web.controller.BaseController;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"test/importPic"})
public class ImportPicController extends BaseController implements Runnable {
    @Resource
    UploadDao uploadDao;
    @Resource
    MemberInfoDao memberInfoDao;
    private Thread thread;
    private boolean isRun = false;
    private int[] total = new int[]{0, 0};
    private int[] donum = new int[]{0, 0};

    public ImportPicController() {
    }

    private Connection getCn() throws Exception {
        return DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.5.18:1433/wills2;charset=CP936", "wills", "wills1234");
    }

    public void run() {
        Connection cn = null;

        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            cn = this.getCn();
            String e = "select top 100 membercode, photo, type, createuser, createtime from (\nselect membercode, photo, 1 type, createuser, createtime\nfrom memberphoto left join wills..tmpphoto on membercode=member and type=1\nwhere member is null\nunion all select membercode, photo, 2 type, createuser, createtime\nfrom idcardphoto left join wills..tmpphoto on membercode=member and type=2\nwhere member is null\n) t order by createtime";
            PreparedStatement pstmt = cn.prepareStatement(e);

            PreparedStatement pstmt2;
            ResultSet rs;
            for(pstmt2 = cn.prepareStatement("insert into wills..tmpphoto values(?, ?, ?)"); this.isRun; rs.close()) {
                rs = pstmt.executeQuery();

                while(rs.next()) {
                    String memberCode = rs.getString("memberCode");
                    int type = rs.getInt("type");
                    Upload file = null;

                    try {
                        file = this.savePic(memberCode + (type == 2?"_idcard":""), this.getData(rs.getBinaryStream("photo")), rs.getString("createuser"), rs.getTimestamp("createtime"));
                        if(type == 2) {
                            this.memberInfoDao.updateIdCardPhoto(memberCode, file.getUrl());
                        } else {
                            this.memberInfoDao.updateMemberPhoto(memberCode, file.getUrl());
                        }
                    } catch (Exception var19) {
                        this.log.error("import error - " + memberCode, var19);
                    }

                    pstmt2.setString(1, memberCode);
                    pstmt2.setInt(2, type);
                    pstmt2.setInt(3, file == null?0:file.getFileId().intValue());
                    pstmt2.executeUpdate();
                    ++this.donum[type - 1];
                    if(!this.isRun) {
                        break;
                    }
                }
            }

            pstmt2.close();
            pstmt.close();
        } catch (Exception var20) {
            var20.printStackTrace();
        } finally {
            if(cn != null) {
                try {
                    cn.close();
                } catch (SQLException var18) {
                    ;
                }
            }

            this.isRun = false;
        }

    }

    private byte[] getData(InputStream is) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        int n;
        while((n = is.read(buf)) != -1) {
            os.write(buf, 0, n);
        }

        is.close();
        return os.toByteArray();
    }

    private Upload savePic(String name, byte[] data, String user, Date time) throws Exception {
        int wid = 0;
        int hei = 0;

        try {
            ByteArrayInputStream upload = new ByteArrayInputStream(data);
            BufferedImage path = ImageIO.read(upload);
            wid = path.getWidth((ImageObserver)null);
            hei = path.getHeight((ImageObserver)null);
        } catch (Exception var12) {
            this.log.error(var12);
        }

        Upload upload1 = new Upload();
        upload1.setFileName(name);
        upload1.setUrl("");
        upload1.setExt("jpg");
        upload1.setWidth(Integer.valueOf(wid));
        upload1.setHeight(Integer.valueOf(hei));
        upload1.setSize(Integer.valueOf(data.length));
        upload1.setUploadUser(user);
        upload1.setUploadTime(time);
        this.uploadDao.addUpload(upload1);
        String path1 = this.getSetting("upload.path");
        String fn = upload1.getUrl();
        File dir = new File(path1 + fn.substring(0, fn.lastIndexOf("/") + 1));
        if(!dir.exists()) {
            dir.mkdirs();
        }

        FileOutputStream fos = new FileOutputStream(path1 + fn);
        fos.write(data);
        fos.close();
        return upload1;
    }

    private void getstat() {
        Connection cn = null;

        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            cn = this.getCn();
            Statement e = cn.createStatement();
            String sql = "select count(*),sum(case when member is null then 0 else 1 end)\nfrom memberphoto left join wills..tmpphoto on membercode=member and type=1";
            ResultSet rs = e.executeQuery(sql);
            if(rs.next()) {
                this.total[0] = rs.getInt(1);
                this.donum[0] = rs.getInt(2);
            }

            rs.close();
            sql = "select count(*),sum(case when member is null then 0 else 1 end)\nfrom idcardphoto left join wills..tmpphoto on membercode=member and type=2";
            rs = e.executeQuery(sql);
            if(rs.next()) {
                this.total[1] = rs.getInt(1);
                this.donum[1] = rs.getInt(2);
            }

            rs.close();
            e.close();
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            if(cn != null) {
                try {
                    cn.close();
                } catch (SQLException var12) {
                    ;
                }
            }

            this.isRun = false;
        }

    }

    @RequestMapping({"/"})
    public ModelAndView index() {
        if(this.total[0] == 0) {
            this.getstat();
        }

        ModelAndView mv = new ModelAndView("test/import/pic");
        mv.addObject("total", Integer.valueOf(this.total[0]));
        mv.addObject("donum", Integer.valueOf(this.donum[0]));
        mv.addObject("total2", Integer.valueOf(this.total[1]));
        mv.addObject("donum2", Integer.valueOf(this.donum[1]));
        return mv;
    }

    @RequestMapping({"stat"})
    @ResponseBody
    public String stat() {
        String str = this.donum[0] + "+-" + this.donum[1] + "/" + this.total[0] + "+" + this.total[1];
        return this.isRun?str + " 正在导入":str;
    }

    @RequestMapping({"_start"})
    @ResponseBody
    public String _start() {
        if(this.isRun) {
            return this.getResult(true, "正在运行");
        } else {
            this.isRun = true;
            this.thread = new Thread(this);
            this.thread.setDaemon(true);
            this.thread.start();
            return this.getResult(true, "开始运行");
        }
    }

    @RequestMapping({"_stop"})
    @ResponseBody
    public String _stop() {
        if(!this.isRun) {
            return this.getResult(true, "尚未运行");
        } else {
            this.isRun = false;
            return this.getResult(true, "等待停止");
        }
    }
}